home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Media / EffectEdit / Simple.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  4.3 KB  |  166 lines

  1. //
  2. // Simple Lighting Model
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // Note: This effect file works with EffectEdit.
  6. //
  7.  
  8. string XFile = "tiger\\tiger.x";   // model
  9. int    BCLR = 0xff202080;          // background
  10.  
  11. // light direction (view space)
  12. float3 lightDir <  string UIDirectional = "Light Direction"; > = {0.577, -0.577, 0.577};
  13.  
  14. // light intensity
  15. float4 I_a = { 0.1f, 0.1f, 0.1f, 1.0f };    // ambient
  16. float4 I_d = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
  17. float4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
  18.  
  19. // material reflectivity
  20. float4 k_a : MATERIALAMBIENT = { 1.0f, 1.0f, 1.0f, 1.0f };    // ambient
  21. float4 k_d : MATERIALDIFFUSE = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
  22. float4 k_s : MATERIALSPECULAR= { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
  23. float  n   : MATERIALPOWER = 32.0f;                           // power
  24.  
  25. // texture
  26. texture Tex0 < string name = "tiger\\tiger.bmp"; >;
  27.  
  28. // transformations
  29. float4x4 World      : WORLD;
  30. float4x4 View       : VIEW;
  31. float4x4 Projection : PROJECTION;
  32.  
  33. struct VS_OUTPUT
  34. {
  35.     float4 Pos  : POSITION;
  36.     float4 Diff : COLOR0;
  37.     float4 Spec : COLOR1;
  38.     float2 Tex  : TEXCOORD0;
  39. };
  40.  
  41. VS_OUTPUT VS(
  42.     float3 Pos  : POSITION, 
  43.     float3 Norm : NORMAL, 
  44.     float2 Tex  : TEXCOORD0)
  45. {
  46.     VS_OUTPUT Out = (VS_OUTPUT)0;
  47.  
  48.     float3 L = -lightDir;
  49.  
  50.     float4x4 WorldView = mul(World, View);
  51.  
  52.     float3 P = mul(float4(Pos, 1), (float4x3)WorldView);  // position (view space)
  53.     float3 N = normalize(mul(Norm, (float3x3)WorldView)); // normal (view space)
  54.  
  55.     float3 R = normalize(2 * dot(N, L) * N - L);          // reflection vector (view space)
  56.     float3 V = -normalize(P);                             // view direction (view space)
  57.  
  58.     Out.Pos  = mul(float4(P, 1), Projection);             // position (projected)
  59.     Out.Diff = I_a * k_a + I_d * k_d * max(0, dot(N, L)); // diffuse + ambient
  60.     Out.Spec = I_s * k_s * pow(max(0, dot(R, V)), n/4);   // specular
  61.     Out.Tex  = Tex;                                       
  62.  
  63.     return Out;
  64. }
  65.  
  66. sampler Sampler = sampler_state
  67. {
  68.     Texture   = (Tex0);
  69.     MipFilter = LINEAR;
  70.     MinFilter = LINEAR;
  71.     MagFilter = LINEAR;
  72. };
  73.  
  74. float4 PS(
  75.     float4 Diff : COLOR0,
  76.     float4 Spec : COLOR1,
  77.     float2 Tex  : TEXCOORD0) : COLOR
  78. {
  79.     return tex2D(Sampler, Tex) * Diff + Spec;
  80. }
  81.  
  82. technique TVertexAndPixelShader
  83. {
  84.     pass P0
  85.     {
  86.         // shaders
  87.         VertexShader = compile vs_1_1 VS();
  88.         PixelShader  = compile ps_1_1 PS();
  89.     }  
  90. }
  91.  
  92. technique TVertexShaderOnly
  93. {
  94.     pass P0
  95.     {
  96.         // lighting
  97.         Lighting       = FALSE;
  98.         SpecularEnable = TRUE;
  99.  
  100.         // samplers
  101.         Sampler[0] = (Sampler);
  102.  
  103.         // texture stages
  104.         ColorOp[0]   = MODULATE;
  105.         ColorArg1[0] = TEXTURE;
  106.         ColorArg2[0] = DIFFUSE;
  107.         AlphaOp[0]   = MODULATE;
  108.         AlphaArg1[0] = TEXTURE;
  109.         AlphaArg2[0] = DIFFUSE;
  110.  
  111.         ColorOp[1]   = DISABLE;
  112.         AlphaOp[1]   = DISABLE;
  113.  
  114.         // shaders
  115.         VertexShader = compile vs_1_1 VS();
  116.         PixelShader  = NULL;
  117.     }
  118. }
  119.  
  120. technique TNoShader
  121. {
  122.     pass P0
  123.     {
  124.         // transforms
  125.         WorldTransform[0]   = (World);
  126.         ViewTransform       = (View);
  127.         ProjectionTransform = (Projection);
  128.  
  129.         // material
  130.         MaterialAmbient  = (k_a); 
  131.         MaterialDiffuse  = (k_d); 
  132.         MaterialSpecular = (k_s); 
  133.         MaterialPower    = (n);
  134.         
  135.         // lighting
  136.         LightType[0]      = DIRECTIONAL;
  137.         LightAmbient[0]   = (I_a);
  138.         LightDiffuse[0]   = (I_d);
  139.         LightSpecular[0]  = (I_s); 
  140.         LightDirection[0] = (lightDir);
  141.         LightRange[0]     = 100000.0f;
  142.  
  143.         LightEnable[0] = TRUE;
  144.         Lighting       = TRUE;
  145.         SpecularEnable = TRUE;
  146.         
  147.         // samplers
  148.         Sampler[0] = (Sampler);
  149.         
  150.         // texture stages
  151.         ColorOp[0]   = MODULATE;
  152.         ColorArg1[0] = TEXTURE;
  153.         ColorArg2[0] = DIFFUSE;
  154.         AlphaOp[0]   = MODULATE;
  155.         AlphaArg1[0] = TEXTURE;
  156.         AlphaArg2[0] = DIFFUSE;
  157.  
  158.         ColorOp[1]   = DISABLE;
  159.         AlphaOp[1]   = DISABLE;
  160.  
  161.         // shaders
  162.         VertexShader = NULL;
  163.         PixelShader  = NULL;
  164.     }
  165. }
  166.